﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace TheOliver.Controls
{
	public partial class AnalogClock : UserControl 
	{
        DispatcherTimer _timer;

		public AnalogClock()
		{
            InitializeComponent();

            this.Loaded += AnalogClock_Loaded;
		}

        void AnalogClock_Loaded(object sender, RoutedEventArgs e)
        {
            _timer = new DispatcherTimer();
            _timer.Tick += _timer_Tick;
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 250);
            _timer.Start();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            UpdateTime();
        }

        //public DateTime? TimeOffset
        //{

        //}

        private void UpdateTime()
        {
            double degree = 360 / 60;

            // Sekunden
            RotateTransform rt = new RotateTransform();
            rt.CenterX = 1;
            rt.CenterY = 95;
            rt.Angle = degree * DateTime.Now.Second;
            _secondHand.RenderTransform = rt;

            // Minuten 
            rt = new RotateTransform();
            rt.CenterX = 2.5;
            rt.CenterY = 80;
            rt.Angle = degree * DateTime.Now.Minute;
            _minuteHand.RenderTransform = rt;

            // Stunden
            rt = new RotateTransform();
            rt.CenterX = 4.5;
            rt.CenterY = 60;
            double angle = 360.0 / 12.0 * DateTime.Now.Hour;
            angle += 360.0 / (12.0 * 60.0) * DateTime.Now.Minute;
            rt.Angle = angle;
            _hourHand.RenderTransform = rt;
        }
	}
}